/**
 * 根据id、parentId 构建树结构数据
 * @param nodeList list 数据
 * @returns {[]} 树结构数据
 */
const treeBuild = (nodeList) => {
  const treeList = [];
  nodeList.forEach(node => {
    if (node.parentId === null || node.parentId === 0 || node.parentId === '0') {
      treeList.push(node)
    }

    nodeList.forEach(nodeChildren => {
      if (nodeChildren.parentId === node.id) {
        node.children.push(nodeChildren);
      }
    });
  });

  return treeList;
};

/**
 * 将dataList转为树形结构数据
 * @param dataList dataList
 * @param params 构建树的参数
 *    keyLabel: 树结构数据的key名
 *    key: dataList数据中的主键字段名称
 *    titleLabel: 树结构数据的title名
 *    title: dataList数据中的显示名称的字段名称
 * @returns {[]|*[]}
 */
export const buildTreeData = ( dataList, params = {keyLabel:'key', key:'id', titleLabel:'title', title:'name'} ) => {
  if (!Array.isArray(dataList)) return [];

  const {keyLabel, key, titleLabel, title} =  { ...{keyLabel:'key', key:'id', titleLabel:'title', title:'name'}, ...params};

  const nodeList = [];
  dataList.forEach(node => {
    const nodeData = { ...node, ...{children: [] }};
    nodeData[keyLabel] = node[key];
    nodeData[titleLabel] = node[title];

    nodeList.push(nodeData);
  });

  return treeBuild(nodeList)
};

/**
 * 将树形结构数据包装成需要的结构数据
 * @param {ListTreeData} ListTreeData  ListTreeData
 * @param {params} params 构建树的参数
 *  disabledKey: 禁用的节点，禁用的节点下的子节点也被禁用
 * @returns 树结构数据
 */
export const treeDataWrapper = ( ListTreeData, params = {keyLabel:'key', key:'id', titleLabel:'title', title:'name', disabledKey: null} ) => {
  if (!Array.isArray(ListTreeData)) return;

  const {keyLabel, key, titleLabel, title, disabledKey} =  { 
    ...{keyLabel:'key', key:'id', titleLabel:'title', title:'name', disabledKey: null}, 
    ...params
  };

  ListTreeData.forEach(node => {
    node[keyLabel] = node[key];
    node[titleLabel] = node[title];

    if (disabledKey === node[key]) {
      disabledNode(node);
    }

    if(node.children !== null && node.children.length > 0) {
      treeDataWrapper(node.children, params);
    }
  });
}

const disabledNode = (ListTreeData) => {
  ListTreeData['disabled'] = true;

  if (ListTreeData.children !== null && ListTreeData.children.length > 0) {
    ListTreeData.children.forEach(node => {
      disabledNode(node);
    });
  }
}